package com.itheima.consumer.listener;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.cglib.core.Local;
import org.springframework.stereotype.Component;

import java.time.LocalTime;

@Component
@Slf4j
public class SpringRabbitListener {
    //监听simple.queue队列
    @RabbitListener(queues = "simple.queue")
    public void listenSimpleQueue(String message) {
        log.info("消费者接收到simple.queue的消息：{}", message);
        System.out.println(message);
    }


    /*
    实现两个消费 work.queue的监听消费消息的方法；
    一个方法消费后沉睡 20毫秒；一个消息消费后沉睡200毫秒；
     */
    @RabbitListener(queues = "work.queue")
    public void listenWorkQueue1(String message) throws InterruptedException {
        System.out.println("消费者1---->" + message + " " + LocalTime.now());
        try {
            Thread.sleep(20);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @RabbitListener(queues = "work.queue")
    public void listenWorkQueue2(String message) throws InterruptedException {
        System.out.println("消费者2---->" + message + " " + LocalTime.now());
        try {
            Thread.sleep(200);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /*
    监听 fanout.queue1 队列的消息
     */
    @RabbitListener(queues = "fanout.queue1")
    public void listenFanoutQueue1(String message) {
        log.info("消费者1---->接收到fanout.queue1的消息：{}", message);
    }

    /*
    监听 fanout.queue2 队列的消息
     */
    @RabbitListener(queues = "fanout.queue2")
    public void listenFanoutQueue2(String message) {
        log.info("消费者2---->接收到fanout.queue2的消息：{}", message);
    }

    /*
    监听 direct.queue1 队列的消息
     */
    @RabbitListener(queues = "direct.queue1")
    public void listenDirectQueue1(String message) {
        System.out.println("【消费者1】接收到消息: " + message);
    }

    /*
    监听 direct.queue2 队列的消息
     */
    @RabbitListener(queues = "direct.queue2")
    public void listenDirectQueue2(String message) {
        System.out.println("【消费者2】接收到消息: " + message);
    }

    /*
监听 topic.queue1 队列的消息111111111111111111111111111111111111111111111111111
 */
    @RabbitListener(queues = "topic.queue1")
    public void listenTopicQueue1(String message) {
        System.out.println("【消费者1】接收到消息: " + message );
    }
    /*
    监听 topic.queue2 队列的消息
     */
    @RabbitListener(queues = "topic.queue2")
    public void listenTopicQueue2(String message) {
        System.out.println("【消费者2】接收到消息: " + message );
    }
}
